package com.tianpengtech.admin.user;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.admin.AbstractAdminController;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.InitInterceptor;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.util.JsonUtil;

/**
* 路由配置地址：add("/admin/user", UserAdminController.class, "/admin/user");
* 数据库映射地址：arp.addMapping(User.dao.tableName(),User.class);
*/
@Before(InitInterceptor.class)
public class UserAdminController extends AbstractAdminController{
	private IAdminOpt opt = new UserAdminServiceImpl();
	@Override
	protected void config() {
		this.clazz=User.class;
		this.service=new UserAdminServiceImpl();
	}

	@Override
	public void beforeAdd() {

	}

	@Override
	public void beforeAdd(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model< ? > model) {

	}
	
	/**
	 * 锁定
	 */
	public void  lock(){
		int id = getParaToInt(User.ID,0);
		try {
			opt.changeStatus(getModel(User.class), id, User.USER_STATUS_LOCK, null);
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}

	
	/**
	 * 解锁
	 */
	public void  unlock(){
		int id = getParaToInt(User.ID,0);
		try {
			opt.changeStatus(getModel(User.class), id, User.USER_STATUS_NORMAL, null);
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}

	@Override
	public void parsePageRecord(Object results) {
		User user = (User)results;
		user.set(User.STATUS, user.status());
	}
}

	